<template>
  <div>
    <div class="search">
      <el-form :inline="true" :model="form" ref="formRef">
        <el-form-item label="工人姓名" style="margin-left: 10px">
          <el-input placeholder="请输入工人姓名" v-model="workerName"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="info" plain style="margin-left: 10px" @click="load(1)">查询</el-button>
          <el-button type="warning" plain style="margin-left: 10px" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="operation">
      <!-- <el-button type="primary" plain @click="handleAdd">新增</el-button> -->
      <el-button type="primary" plain @click="exportExcelSelect">导出</el-button>
    </div>

<!--    表单区域-->
    <div class="table">
      <el-table :data="tableData" stripe border @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="workerId" label="序号" width="80" align="center" sortable></el-table-column>
        <el-table-column prop="workerName" label="姓名" show-overflow-tooltip>
          <!--          设置姓名那一列的数据点击跳转-->
          <template slot-scope="scope">
            <el-button type="text" @click="handleEdit1(scope.row)">{{ scope.row.workerName }}</el-button>
            <!-- <router-link to="/workerInformation"></router-link> -->
          </template>
        </el-table-column>
        <el-table-column prop="gender" label="性别" :formatter="genderFormatter"></el-table-column>
        <el-table-column prop="identityCard" label="身份证号"></el-table-column>
        <el-table-column prop="age" label="年龄"></el-table-column>
        <el-table-column prop="remunerationMethod" label="计酬方式"></el-table-column>
        <el-table-column prop="workerPhone" label="联系方式"></el-table-column>
        <el-table-column prop="wageScale" label="工资标准(元)"></el-table-column>
        <el-table-column prop="cardNumber" label="工资卡号"></el-table-column>
        <el-table-column prop="bank" label="开户行"></el-table-column>

        <el-table-column label="操作" align="center" width="180">
          <template v-slot="scope">
            <el-button size="mini" type="primary" plain @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" plain @click="del(scope.row.workerId)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

<!--      新增工人的弹窗-->
      <el-dialog title="信息" :visible.sync="fromVisible" width="40%" :close-on-click-modal="false" destroy-on-close>
        <el-form label-width="100px" style="padding-right: 50px" :model="form" :rules="rules" ref="formRef">
          <el-form-item prop="unitId" label="参与单位">
            <el-select v-model="form.unitId" placeholder="请选择" style="width: 100%">
              <el-option label="选项一" value="project1"></el-option>
            <el-option label="选项二" value="project2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="bank" label="开户行">
            <el-select v-model="form.bank" placeholder="请选择" style="width: 100%">
              <el-option label="选项一" value="project1"></el-option>
            <el-option label="选项二" value="project2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="teamsId" label="班组名称">
            <el-select v-model="form.teamsId" placeholder="请选择" style="width: 100%">
              <el-option label="选项一" value="project1"></el-option>
            <el-option label="选项二" value="project2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="workerName" label="工人姓名">
            <el-input v-model="form.workerName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item prop="workerPhone" label="联系方式">
            <el-input v-model="form.workerPhone" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item prop="remunerationMethod" label="计酬方式">
            <el-input v-model="form.remunerationMethod" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item prop="wageScale" label="工资标准">
            <el-input v-model="form.wageScale" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item prop="cardNumber" label="卡号">
            <el-input v-model="form.cardNumber" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item prop="identityCard" label="身份证号">
            <el-input v-model="form.identityCard" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="性别:" prop="gender">
            <el-radio v-model="radio" label="1">男</el-radio>
            <el-radio v-model="radio" label="2">女</el-radio>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="fromVisible = false">取 消</el-button>
          <el-button type="primary" @click="save">确 定</el-button>
        </div>
      </el-dialog>

<!--      预览弹窗表格-->
      <el-dialog title="表格保存预览" width="70%" :visible.sync="selectWindow" align="center">
        <el-table :data="selectData" id="selectTable" height="380px" border>
          <el-table-column prop="workerId" label="序号" width="80" align="center"></el-table-column>
        <el-table-column prop="workerName" label="姓名"></el-table-column>
        <el-table-column prop="gender" label="性别"></el-table-column>
        <el-table-column prop="identityCard" label="身份证号"></el-table-column>
        <el-table-column prop="age" label="年龄"></el-table-column>
        <el-table-column prop="remunerationMethod" label="计酬方式"></el-table-column>
        <el-table-column prop="workerPhone" label="联系方式"></el-table-column>
        <el-table-column prop="wageScale" label="工资标准(元)"></el-table-column>
        <el-table-column prop="cardNumber" label="工资卡号"></el-table-column>
        <el-table-column prop="bank" label="开户行"></el-table-column>
        </el-table>
        <div slot="footer" class="dialog-footer">
          <el-button @click="selectWindow = false">取 消</el-button>
          <el-button type="primary" @click="exportExcel">确定保存</el-button>
        </div>
      </el-dialog>

      <!-- 分页区域-->
      <div class="pagination">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                       :current-page="pageNum" :page-sizes="[1, 3, 5, 10]" :page-size="pageSize"
                       layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
      </div>

  </div>
</template>

<script>
import FileSaver from "file-saver";
import XLSX from "xlsx";

export default {
  name: "ClickTeam.vue",
  data() {
    return {
      tableData: [],  // 所有的数据
      pageNum: 1,   // 当前的页码
      pageSize: 10,  // 每页显示的个数
      total: 0,
      workerName: null,
      fromVisible: false,
      teamsId: this.$route.query.param.teamsId,
      form: {
        teamsId: this.$route.query.param.teamsId,
      },
      user: JSON.parse(localStorage.getItem('xm-user') || '{}'),
      ids: [],
      departmentData: [],
     //表格中选中的数据
      selectData: [],
      //预览表格的弹窗
      selectWindow: false,
      radio:'1',
    }
  },
  created() {
    this.load(1)
    this.loadDepartments()
  },
  methods: {
    loadDepartments() {
      var teamsId1 = this.teamsId;
      this.$request.get('/ByteamsId/' + teamsId1).then(res => {
        if (res.code === '200') {
          this.departmentData = res.data.rows;
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    // handleAdd() {   // 新增数据
    //   this.form = {}  // 新增数据的时候清空数据
    //   this.fromVisible = true   // 打开弹窗
    // },
    handleEdit(row) {   // 编辑数据
      this.form = JSON.parse(JSON.stringify(row))  // 给form对象赋值  注意要深拷贝数据
      this.fromVisible = true   // 打开弹窗
    },
    handleEdit1(row) {   // 编辑数据
      this.form = JSON.parse(JSON.stringify(row))  // 给form对象赋值  注意要深拷贝数据
      this.$router.push({
        name: "WorkerInformation",
        query: {param: this.form},
      })
    },
    //监听pagesize改变的事件
    handleSizeChange(val) {
      this.pageSize=val
      this.loadDepartments()
    },
    handleCurrentChange(pageNum) {
      this.load(pageNum)
    },
    save() {   // 保存按钮触发的逻辑  它会触发新增或者更新
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          this.$request({
            url: this.form.workerId ? '/workerInfoUpdate' : '/workerInsert',
            method: this.form.workerId ? 'PUT' : 'POST',
            data: this.form
          }).then(res => {
            if (res.code === '200') {  // 表示成功保存
              this.$message.success('保存成功')
              this.load(1)
              this.fromVisible = false
            } else {
              this.$message.error(res.msg)  // 弹出错误的信息
            }
          })
        }
      })
    },
    del(id) {   // 单个删除
      this.$confirm('您确定删除吗？', '确认删除', {type: "warning"}).then(response => {
        this.$request.delete('/worker/' + id).then(res => {
          if (res.code === '200') {   // 表示操作成功
            this.$message.success('操作成功')
            this.load(1)
          } else {
            this.$message.error(res.msg)  // 弹出错误的信息
          }
        })
      }).catch(() => {
      })
    },
    // handleSelectionChange(rows) {   // 当前选中的所有的行数据
    //   this.ids = rows.map(v => v.id)   //  [1,2]
    // },
    load(pageNum) {  // 分页查询
      var teamsId1 = this.teamsId;
      if (pageNum) this.pageNum = pageNum
      this.$request.get('/ByteamsId/' + teamsId1, {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          workerName: this.workerName,
        }
      }).then(res => {
        this.tableData = res.data?.rows
        this.total = res.data?.total
      })
    },
    reset() {
      this.workerName = null
      this.load(1)
    },
    //格式化数据
    getExcel(dom, title = '默认标题',) {
      var excelTitle = title;
      var wb = XLSX.utils.table_to_book(document.querySelector(dom));
      /* 获取二进制字符串作为输出 */
      var wbout = XLSX.write(wb, { bookType: "xlsx", bookSST: true, type: "array" });
      try {
        FileSaver.saveAs(
            new Blob([wbout], { type: "application/octet-stream" }),
            excelTitle + ".xlsx"
        );
      } catch (e) {
        if (typeof console !== "undefined") console.log(e, wbout);
      }
      return wbout;
    },
    //导出
    exportExcel() {
      this.getExcel('#selectTable', '工人表')
    },
    //显示预览弹窗
    exportExcelSelect() {
      if (this.selectData.length < 1) {
        this.$message.error('请选择要导出的内容！');
        return false;
      }
      this.selectWindow = true;
    },
    //选中数据
    handleSelectionChange(val) {
      this.selectData = val;
    },
    genderFormatter(row, column) {
      const genderValue = row[column.property];
      if (genderValue === 0) {
        return '女';
      } else if (genderValue === 1) {
        return '男';
      }
      return genderValue;
    },
  }
}
</script>

<style scoped>


</style>